#include "workerManager.h"

WorkerManager::WorkerManager()
{
    //初始化属性
    this->num = 0;
    this->empArray = NULL;

    //1、文件不存在
    ifstream ifs;
    ifs.open(filename, ios::in);

    if (!ifs.is_open())
    {
        cout << "文件不存在" << endl;

        //初始化属性
        //初始化记录人数
        this->num = 0;
        //初始化数组指针
        this->empArray = NULL;
        //初始化文件是否为空
        this->FileIsEmpty = true;
        
        ifs.close();
        return;
    }

    //2、文件存在，并且记录为空
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        //文件为空
        cout << "当前您的公司还没有员工" << endl;
        
        //初始化属性
        //初始化记录人数
        this->num = 0;
        //初始化数组指针
        this->empArray = NULL;
        //初始化文件是否为空
        this->FileIsEmpty = true;

        ifs.close();
        return;
    }

    //3、文件存在，并且有记录
    int empnum = this->getNum();
    cout << "当前职工人数为：" << empnum << endl;
    this->num = empnum;

    //开辟空间
    this->empArray = new Worker * [this->num];
    //将文件中的数据存到数组中
    this->init_Emp();

}

void WorkerManager::showMenu()
{
    cout << endl;
    cout << "*********************" << endl;
    cout << "!欢迎来到职工管理系统！" << endl;
    cout << "***0：退出管理程序***" << endl;
    cout << "***1：增加职工信息***" << endl;
    cout << "***2：显示职工信息***" << endl;
    cout << "***3：删除离职职工***" << endl;
    cout << "***4：修改职工信息***" << endl;
    cout << "***5：查找职工信息***" << endl;
    cout << "***6：按照编号排序***" << endl;
    cout << "***7：清空所有文档***" << endl;
    cout << "*********************" << endl;
    cout << endl;
}

//退出系统
void WorkerManager::ExitSystem()
{
    cout << "欢迎下次使用" << endl;
    exit(0);
}


//添加职工
void WorkerManager::addEmp()
{
    cout << "请输入添加职工的数量：";

    int addnum = 0;
    cin >> addnum;//保存用户的输入数量

    if (addnum >= 0)
    {
        //添加
        //计算添加新空间大小
        int newSize = this->num + addnum;//新空间人数 = 原人数 + 新人数；

        //开辟新空间
        Worker** newSpace = new Worker * [newSize];

        //将原来空间下的数据，拷贝到新空间下
        if (this->empArray != NULL)
        {
            for (int i = 0;i < this->num;i++)
            {
                newSpace[i] = empArray[i];
            }
        }
        //批量添加新数据
        for (int i = 0;i < addnum;i++)
        {
            int id;     //职工编号
            string name;//职工姓名
            int dSelect;//职工的岗位

            cout << "请输入第" << i + 1 << "个新职工的编号：" << endl;
            cin >> id;

            cout << "请输入第" << i + 1 << "个新职工的姓名：" << endl;
            cin >> name;

            cout << "请选择第" << i + 1 << "个新职工的岗位：" << endl;
            cout << "1、普通员工" << endl;
            cout << "2、经理" << endl;
            cout << "3、总裁" << endl;
            cin >> dSelect;

            Worker* worker = NULL;
            switch (dSelect)
            {
            case 1:
                worker = new Employee(id, name, dSelect);
                break;
            case 2:
                worker = new Manager(id, name, dSelect);
                break;
            case 3:
                worker = new Boss(id, name, dSelect);
                break;
            }

            //将创建职工指针，保存到数组中
            newSpace[this->num + i] = worker;
        }

        //释放原有空间
        delete[] this->empArray;

        //更改新空间指向
        this->empArray = newSpace;

        //更新员工数量
        this->num = newSize;

        cout << "成功添加了" << addnum << "名职工！" << endl;

        //更新文件不为空标志
        this->FileIsEmpty = false;

        //将数据保存到文件中
        this->SaveFile();
    }
    else {
        cout << "输入的数据无效！" << endl;
    }
}

//保存文件
void WorkerManager::SaveFile()
{
    ofstream ofs;
    ofs.open(filename, ios::out);//写文件

    for (int i = 0;i < num;i++)
    {
        ofs << this->empArray[i]->ID << " "
            << this->empArray[i]->name << " "
            << this->empArray[i]->deptID << endl;
    }

    //关闭文件
    ofs.close();

}

//统计文件中存储的人数
int WorkerManager::getNum()
{
    ifstream ifs;
    ifs.open(filename, ios::in);//读文件

    int id;
    string name;
    int did;

    int num = 0;
    while (ifs >> id && ifs >> name && ifs >> did)
    {
        //统计人数
        num++;
    }
    return num;
}

//初始化员工
void WorkerManager::init_Emp()
{
    ifstream ifs;
    ifs.open(filename, ios::in);

    int index = 0;
    int id;
    string name;
    int did;

    while (ifs >> id && ifs >> name && ifs >> did)
    {
        Worker* worker = NULL;

        if (did == 1)
        {
            worker = new Employee(id, name, did);
        }
        else if (did == 2)
        {
            worker = new Manager(id, name, did);
        }
        else if (did == 3)
        {
            worker = new Boss(id, name, did);
        }
        this->empArray[index] = worker;
        index++;
    }

    ifs.close();
}

//显示职工
void WorkerManager::show_Emp()
{
    //判断文件是否为空
    if (this->FileIsEmpty)
    {
        cout << "当前您的公司还没有员工" << endl;
    }
    else
    {
        for (int i = 0;i < this->num;i++)
        {
            this->empArray[i]->showInfo();
        }
    }
}

//删除职工
void WorkerManager::del_Emp()
{
    if (this->FileIsEmpty)
    {
        cout << "当前您的公司还没有员工" << endl;
    }
    else
    {
        //按照职工编号删除
        cout << "请输入想要删除的职工编号：" << endl;
        int id = 0;
        cin >> id;

        int index = this->isExist(id);

        if (index != -1)//职工存在，可以删除
        {
            //数据前移
            for (int i = index;i < this->num - 1;i++)
            {
                this->empArray[i] = this->empArray[i + 1];
            }
            this->num--;//更新人员个数

            //数据同步更新至文件中
            this->SaveFile();

            cout << "删除成功！" << endl;
        }
        else {
            cout << "您公司没有编号为" << id << "的职工" << endl;
        }
    }
}

//判断某职工是否存在，如果存在返回职工在数组中的存储位置，不存在则返回-1
int WorkerManager::isExist(int id)
{
    int index = -1;
    for (int i = 0;i < this->num;i++)
    {
        if (this->empArray[i]->ID == id)
        {
            //找到职工
            index = i;
            break;
        }
    }
    return index;
}

//修改职工
void WorkerManager::mod_Emp()
{
    if (this->FileIsEmpty)
    {
        cout << "当前您的公司还没有员工" << endl;
    }
    else
    {
        cout << "请输入您要修改的职工编号：" << endl;
        int id;
        cin >> id;

        int ret = this->isExist(id);
        if (ret != -1)
        {
            //查找到这个编号的职工
            int newID;
            string newname;
            int dSelect;

            cout << "查到" << id << "号职工" << endl;
            cout << "请输入新的职工编号：" << endl;
            cin >> newID;
            
            cout << "请输入新的职工姓名：" << endl;
            cin >> newname;
            
            cout << "请输入新的职工岗位：" << endl;
            cout << "1、普通员工" << endl;
            cout << "2、经理" << endl;
            cout << "3、总裁" << endl;
            cin >> dSelect;

            Worker* worker = NULL;
            switch (dSelect)
            {
            case 1:
                worker = new Employee(newID, newname, dSelect);
                break;
            case 2:
                worker = new Manager(newID, newname, dSelect);
                break;
            case 3:
                worker = new Boss(newID, newname, dSelect);
                break;
            }

            //将创建职工指针，保存到数组中
            this->empArray[ret] = worker;

            cout << "修改成功！" << endl;

            //保存到文件中
            this->SaveFile();
        }
        else
        {
            cout << "查无此人，修改失败！" << endl;
        }
    }
}

//查找职工
void WorkerManager::find_Emp()
{
    if (this->FileIsEmpty)
    {
        cout << "当前您的公司还没有员工" << endl;
    }
    else
    {
        cout << "请输入查找的方式：" << endl;
        cout << "1、按职工编号查找" << endl;
        cout << "2、按职工姓名查找" << endl;

        int select = 0;
        cin >> select;

        //按照编号来查找
        if (select == 1)
        {
            
            int id;
            cout << "请输入要查找的职工编号：" << endl;
            cin >> id;

            int ret = isExist(id);
            if (ret != -1)
            {
                //找到该职工
                cout << "查找成功！该职工的信息如下：" << endl;
                this->empArray[ret]->showInfo();
            }
            else
            {
                //未找到该职工
                cout << "查找失败，查无此人！" << endl;
            }
        }

        //按照姓名来查找
        else if (select == 2)
        {
            string name;
            cout << "请输入要查找的姓名：" << endl;
            cin >> name;

            //判断是否查找成功的标志
            bool flag = false;
            
            for (int i = 0;i < this->num;i++)
            {
                if (this->empArray[i]->name == name)
                {
                    cout << "查找成功！职工编号为"
                        << this->empArray[i]->ID << "的职工信息为：" << endl;

                    this->empArray[i]->showInfo();
                    flag = true;
                }
            }

            if (flag == false)
            {
                //未找到该职工
                cout << "查找失败，查无此人！" << endl;
            }
        }
        
        else {
            cout << "输入的选项有误！" << endl;
        }
    }
}

//排序职工
void WorkerManager::sort_Emp()
{
    if (this->FileIsEmpty)
    {
        cout << "当前您的公司还没有员工" << endl;
    }
    else
    {
        cout << "请选择排序的方式：" << endl;
        cout << "1、按编号升序排列" << endl;
        cout << "2、按编号降序排列" << endl;
        int select = 0;
        cin >> select;

        for (int i = 0;i < this->num;i++)
        {
            int minOrMax = i;//最值下标
            for (int j = i + 1;j < this->num;j++)
            {
                if (select == 1)//升序
                {
                    if (this->empArray[minOrMax]->ID > this->empArray[j]->ID)
                    {
                        minOrMax = j;
                    }
                }
                else//降序
                {
                    if (this->empArray[minOrMax]->ID < this->empArray[j]->ID)
                    {
                        minOrMax = j;
                    }
                }
            }

            if (i != minOrMax)
            {
                //交换数据
                Worker* temp = this->empArray[i];
                this->empArray[i] = this->empArray[minOrMax];
                this->empArray[minOrMax] = temp;
            }
        }
        this->SaveFile();
        cout << "排序成功！" << endl;
    }
}

//清空文件
void WorkerManager::clean_file()
{
    cout << "确定清空吗？" << endl;
    cout << "1、确定" << endl;
    cout << "2、返回" << endl;

    int select = 0;
    cin >> select;

    if (select == 1)
    {
        //确定清空文件
        ofstream ofs(filename, ios::trunc);//删除文件后，再创建

        ofs.close();

        if (this->empArray != NULL)
        {
            //删除堆区的所有职工
            for (int i = 0;i < this->num;i++)
            {
                //delete this->empArray[i];
                this->empArray[i] = NULL;
            }

            //删除堆区的数组指针
            delete[] this->empArray;
            this->empArray = NULL;

            this->num = 0;
            this->FileIsEmpty = true;

            cout << "清空成功！" << endl;
        }
    }
}


//析构函数
WorkerManager::~WorkerManager()
{
    if (this->empArray != NULL)
    {
        delete[] this->empArray;
        this->empArray = NULL;
    }
}